<template>
  <div class="home">
    <!-- 头部导航 -->
    <header class="header">
      <div class="container">
        <div class="logo">
          <h1>拼多多</h1>
        </div>
        <nav class="nav">
          <router-link to="/">首页</router-link>
          <router-link to="/products">商品</router-link>
          <router-link to="/cart">购物车</router-link>
          <template v-if="userStore.isLoggedIn">
            <el-dropdown @command="handleCommand" trigger="click">
              <span class="user-info">
                <el-icon><User /></el-icon>
                {{ userStore.user.username }}
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="admin" v-if="userStore.user.username === 'admin'">管理后台</el-dropdown-item>
                  <el-dropdown-item command="logout" :divided="userStore.user.username === 'admin'">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <router-link to="/login" class="login-link">登录/注册</router-link>
          </template>
        </nav>
      </div>
    </header>

    <!-- Banner区域 -->
    <section class="banner">
      <div class="banner-content">
      </div>
    </section>

    <!-- 服务保障 -->
    <section class="service-guarantee">
      <div class="container">
        <div class="guarantee-list">
          <div class="guarantee-item">
            <div class="guarantee-icon">📦</div>
            <h3>全场包邮</h3>
            <p>满额免运费</p>
          </div>
          <div class="guarantee-item">
            <div class="guarantee-icon">🔄</div>
            <h3>7天退换</h3>
            <p>无忧退换货</p>
          </div>
          <div class="guarantee-item">
            <div class="guarantee-icon">✓</div>
            <h3>假一赔十</h3>
            <p>正品保障</p>
          </div>
          <div class="guarantee-item">
            <div class="guarantee-icon">⚡</div>
            <h3>48小时发货</h3>
            <p>快速配送</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 商品分类 -->
    <section class="categories">
      <div class="container">
        <h2 class="section-title">商品分类</h2>
        <div class="category-list">
          <div 
            v-for="category in categories" 
            :key="category.id"
            class="category-item"
            @click="goToCategory(category.id)"
          >
            <h3>{{ category.name }}</h3>
            <p>{{ category.description }}</p>
          </div>
        </div>
      </div>
    </section>

    <!-- 热门商品 -->
    <section class="products">
      <div class="container">
        <h2 class="section-title">热门商品</h2>
        <div class="product-list">
          <div 
            v-for="product in products.slice(0, 8)" 
            :key="product.id"
            class="product-item"
            @click="goToProduct(product.id)"
          >
            <div class="product-image">
              <img :src="product.image || '/placeholder.png'" :alt="product.name">
            </div>
            <div class="product-info">
              <h3>{{ product.name }}</h3>
              <p class="price">¥{{ product.price }}</p>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- Footer -->
    <footer class="footer">
      <div class="container">
        <div class="footer-content">
          <div class="footer-section">
            <h4>联系我们</h4>
            <p>客服电话：021-53395288</p>
            <p>服务时间：8:00-23:00</p>
          </div>
          <div class="footer-section">
            <h4>关于我们</h4>
            <p><a href="#">帮助中心</a></p>
            <p><a href="#">消费者保障</a></p>
            <p><a href="#">服务协议</a></p>
          </div>
          <div class="footer-section">
            <h4>商家服务</h4>
            <p><a href="#">商家入驻</a></p>
            <p><a href="#">商家热线</a></p>
            <p><a href="#">招采平台</a></p>
          </div>
        </div>
        <div class="footer-bottom">
          <p>&copy; 2024 拼多多. All rights reserved.</p>
          <p>更多商品优惠 尽在拼多多APP</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, ArrowDown } from '@element-plus/icons-vue'
import { useUserStore } from '../stores'
import request from '../utils/request'

const router = useRouter()
const userStore = useUserStore()

const products = ref([])
const categories = ref([])

const goToProduct = (id) => {
  router.push(`/products/${id}`)
}

const goToCategory = (id) => {
  router.push(`/category/${id}`)
}

const handleCommand = (command) => {
  if (command === 'admin') {
    router.push('/admin/products')
  } else if (command === 'logout') {
    userStore.logout()
    ElMessage.success('已退出登录')
    router.push('/')
  }
}

onMounted(async () => {
  userStore.initUser()
  
  try {
    const [productsRes, categoriesRes] = await Promise.all([
      request.get('/products'),
      request.get('/categories')
    ])
    products.value = productsRes.data || []
    categories.value = categoriesRes.data || []
  } catch (error) {
    console.error('加载数据失败:', error)
  }
})
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 15px 0;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo h1 {
  color: #E02E24;
  font-size: 24px;
}

.nav {
  display: flex;
  gap: 30px;
}

.nav a {
  text-decoration: none;
  color: #333;
  font-size: 16px;
  transition: color 0.3s;
}

.nav a:hover,
.nav a.router-link-active {
  color: #E02E24;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #333;
  font-size: 16px;
  cursor: pointer;
  padding: 8px 15px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-info:hover {
  background: #f5f5f5;
  color: #E02E24;
}

.login-link {
  display: inline-block;
  padding: 8px 20px;
  background: linear-gradient(to right, #E02E24, #FF4433);
  color: white !important;
  border-radius: 20px;
  font-size: 14px;
  transition: all 0.3s;
}

.login-link:hover {
  background: linear-gradient(to right, #FF4433, #E02E24);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 103, 0, 0.3);
}

.banner {
  background: url('/images/45945b9d_7fdc_45c0_abc0_258ce8f98f61.jpeg') no-repeat center center;
  background-size: cover;
  color: white;
  padding: 150px 0;
  text-align: center;
  position: relative;
}

.banner::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 1;
}

.banner-content {
  position: relative;
  z-index: 2;
}

.banner-content h2 {
  font-size: 56px;
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.banner-content p {
  font-size: 28px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.service-guarantee {
  background: white;
  padding: 40px 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.guarantee-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 30px;
}

.guarantee-item {
  text-align: center;
  padding: 20px;
  transition: transform 0.3s;
}

.guarantee-item:hover {
  transform: translateY(-5px);
}

.guarantee-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

.guarantee-item h3 {
  font-size: 18px;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.guarantee-item p {
  color: #666;
  font-size: 14px;
}

.section-title {
  font-size: 32px;
  margin: 60px 0 40px;
  text-align: center;
}

.categories {
  padding: 60px 0;
}

.category-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
}

.category-item {
  background: #f5f5f5;
  padding: 30px;
  border-radius: 8px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
}

.category-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.category-item h3 {
  font-size: 20px;
  margin-bottom: 10px;
  color: #333;
}

.category-item p {
  color: #666;
}

.products {
  padding: 60px 0;
  background: #fafafa;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 30px;
}

.product-item {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
}

.product-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.product-image {
  width: 100%;
  height: 200px;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  padding: 20px;
}

.product-info h3 {
  font-size: 16px;
  margin-bottom: 10px;
  color: #333;
}

.price {
  color: #E02E24;
  font-size: 20px;
  font-weight: bold;
}

.footer {
  background: #2c2c2c;
  color: #ccc;
  padding: 50px 0 20px;
  margin-top: 60px;
}

.footer-content {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 40px;
  margin-bottom: 30px;
  padding-bottom: 30px;
  border-bottom: 1px solid #444;
}

.footer-section h4 {
  color: white;
  font-size: 16px;
  margin-bottom: 15px;
  font-weight: 600;
}

.footer-section p {
  margin: 8px 0;
  font-size: 14px;
}

.footer-section a {
  color: #ccc;
  text-decoration: none;
  transition: color 0.3s;
}

.footer-section a:hover {
  color: #E02E24;
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
}

.footer-bottom p {
  margin: 5px 0;
  font-size: 13px;
  color: #999;
}
</style>

